
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.forms.forms &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-forms-forms">
            
  <h1>Source code for django.forms.forms</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Form classes</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">NON_FIELD_ERRORS</span><span class="p">,</span> <span class="n">ValidationError</span>
<span class="c1"># BoundField is imported for backwards compatibility in Django 1.9</span>
<span class="kn">from</span> <span class="nn">django.forms.boundfield</span> <span class="k">import</span> <span class="n">BoundField</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">django.forms.fields</span> <span class="k">import</span> <span class="n">Field</span><span class="p">,</span> <span class="n">FileField</span>
<span class="c1"># pretty_name is imported for backwards compatibility in Django 1.9</span>
<span class="kn">from</span> <span class="nn">django.forms.utils</span> <span class="k">import</span> <span class="n">ErrorDict</span><span class="p">,</span> <span class="n">ErrorList</span><span class="p">,</span> <span class="n">pretty_name</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">django.forms.widgets</span> <span class="k">import</span> <span class="n">Media</span><span class="p">,</span> <span class="n">MediaDefiningClass</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">python_2_unicode_compatible</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">conditional_escape</span><span class="p">,</span> <span class="n">html_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span>

<span class="kn">from</span> <span class="nn">.renderers</span> <span class="k">import</span> <span class="n">get_default_renderer</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;BaseForm&#39;</span><span class="p">,</span> <span class="s1">&#39;Form&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">DeclarativeFieldsMetaclass</span><span class="p">(</span><span class="n">MediaDefiningClass</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Metaclass that collects Fields declared on the base classes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="c1"># Collect fields from current class.</span>
        <span class="n">current_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">attrs</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">Field</span><span class="p">):</span>
                <span class="n">current_fields</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
                <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="n">current_fields</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">creation_counter</span><span class="p">)</span>
        <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;declared_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">(</span><span class="n">current_fields</span><span class="p">)</span>

        <span class="n">new_class</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">DeclarativeFieldsMetaclass</span><span class="p">,</span> <span class="n">mcs</span><span class="p">)</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

        <span class="c1"># Walk through the MRO.</span>
        <span class="n">declared_fields</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">new_class</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">):</span>
            <span class="c1"># Collect fields from base class.</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;declared_fields&#39;</span><span class="p">):</span>
                <span class="n">declared_fields</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">base</span><span class="o">.</span><span class="n">declared_fields</span><span class="p">)</span>

            <span class="c1"># Field shadowing.</span>
            <span class="k">for</span> <span class="n">attr</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">base</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">declared_fields</span><span class="p">:</span>
                    <span class="n">declared_fields</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>

        <span class="n">new_class</span><span class="o">.</span><span class="n">base_fields</span> <span class="o">=</span> <span class="n">declared_fields</span>
        <span class="n">new_class</span><span class="o">.</span><span class="n">declared_fields</span> <span class="o">=</span> <span class="n">declared_fields</span>

        <span class="k">return</span> <span class="n">new_class</span>


<span class="nd">@html_safe</span>
<span class="nd">@python_2_unicode_compatible</span>
<span class="k">class</span> <span class="nc">BaseForm</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="c1"># This is the main implementation of all the Form logic. Note that this</span>
    <span class="c1"># class is different than Form. See the comments by the Form class for more</span>
    <span class="c1"># information. Any improvements to the form API should be made to *this*</span>
    <span class="c1"># class, not to the Form class.</span>
    <span class="n">default_renderer</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">field_order</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">prefix</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">use_required_attribute</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">initial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error_class</span><span class="o">=</span><span class="n">ErrorList</span><span class="p">,</span> <span class="n">label_suffix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">empty_permitted</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">field_order</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">use_required_attribute</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">renderer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span> <span class="o">=</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">files</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">files</span> <span class="o">=</span> <span class="n">files</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auto_id</span> <span class="o">=</span> <span class="n">auto_id</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span> <span class="o">=</span> <span class="n">prefix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial</span> <span class="o">=</span> <span class="n">initial</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span> <span class="o">=</span> <span class="n">error_class</span>
        <span class="c1"># Translators: This is the default suffix added to form field labels</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">label_suffix</span> <span class="o">=</span> <span class="n">label_suffix</span> <span class="k">if</span> <span class="n">label_suffix</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">empty_permitted</span> <span class="o">=</span> <span class="n">empty_permitted</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># Stores the errors after clean() has been called.</span>

        <span class="c1"># The base_fields class attribute is the *class-wide* definition of</span>
        <span class="c1"># fields. Because a particular *instance* of the class might want to</span>
        <span class="c1"># alter self.fields, we create self.fields here by copying base_fields.</span>
        <span class="c1"># Instances should always modify self.fields; they should not modify</span>
        <span class="c1"># self.base_fields.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">base_fields</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bound_fields_cache</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order_fields</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field_order</span> <span class="k">if</span> <span class="n">field_order</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">field_order</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">use_required_attribute</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">use_required_attribute</span> <span class="o">=</span> <span class="n">use_required_attribute</span>

        <span class="c1"># Initialize form renderer. Use a global default if not specified</span>
        <span class="c1"># either as an argument or as self.default_renderer.</span>
        <span class="k">if</span> <span class="n">renderer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_renderer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">renderer</span> <span class="o">=</span> <span class="n">get_default_renderer</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">renderer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_renderer</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">default_renderer</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
                    <span class="n">renderer</span> <span class="o">=</span> <span class="n">renderer</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">renderer</span> <span class="o">=</span> <span class="n">renderer</span>

    <span class="k">def</span> <span class="nf">order_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_order</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Rearranges the fields according to field_order.</span>

<span class="sd">        field_order is a list of field names specifying the order. Fields not</span>
<span class="sd">        included in the list are appended in the default order for backward</span>
<span class="sd">        compatibility with subclasses not overriding field_order. If field_order</span>
<span class="sd">        is None, all fields are kept in the order defined in the class.</span>
<span class="sd">        Unknown fields in field_order are ignored to allow disabling fields in</span>
<span class="sd">        form subclasses without redefining ordering.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">field_order</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">field_order</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fields</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>  <span class="c1"># ignore unknown fields</span>
                <span class="k">pass</span>
        <span class="n">fields</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>  <span class="c1"># add remaining fields in original order</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="n">fields</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">is_valid</span> <span class="o">=</span> <span class="s2">&quot;Unknown&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">is_valid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_errors</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">%(cls)s</span><span class="s1"> bound=</span><span class="si">%(bound)s</span><span class="s1">, valid=</span><span class="si">%(valid)s</span><span class="s1">, fields=(</span><span class="si">%(fields)s</span><span class="s1">)&gt;&#39;</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;cls&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s1">&#39;bound&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span><span class="p">,</span>
            <span class="s1">&#39;valid&#39;</span><span class="p">:</span> <span class="n">is_valid</span><span class="p">,</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="s1">&#39;;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="s2">&quot;Returns a BoundField with the given name.&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span>
                <span class="s2">&quot;Key &#39;</span><span class="si">%s</span><span class="s2">&#39; not found in &#39;</span><span class="si">%s</span><span class="s2">&#39;. Choices are: </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">name</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                    <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">)),</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bound_fields_cache</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_bound_fields_cache</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_bound_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bound_fields_cache</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">errors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns an ErrorDict for the data provided for the form&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">full_clean</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span>

    <span class="k">def</span> <span class="nf">is_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if the form has no errors. Otherwise, False. If errors are</span>
<span class="sd">        being ignored, returns False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span>

    <span class="k">def</span> <span class="nf">add_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the field name with a prefix appended, if this Form has a</span>
<span class="sd">        prefix set.</span>

<span class="sd">        Subclasses may wish to override.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">,</span> <span class="n">field_name</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span> <span class="k">else</span> <span class="n">field_name</span>

    <span class="k">def</span> <span class="nf">add_initial_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add a &#39;initial&#39; prefix for checking dynamic initial values</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s1">&#39;initial-</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_html_output</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">normal_row</span><span class="p">,</span> <span class="n">error_row</span><span class="p">,</span> <span class="n">row_ender</span><span class="p">,</span> <span class="n">help_text_html</span><span class="p">,</span> <span class="n">errors_on_separate_row</span><span class="p">):</span>
        <span class="s2">&quot;Helper function for outputting HTML. Used by as_table(), as_ul(), as_p().&quot;</span>
        <span class="n">top_errors</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">non_field_errors</span><span class="p">()</span>  <span class="c1"># Errors that should be displayed above all fields.</span>
        <span class="n">output</span><span class="p">,</span> <span class="n">hidden_fields</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">html_class_attr</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="n">bf</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="c1"># Escape and cache in local variable.</span>
            <span class="n">bf_errors</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">([</span><span class="n">conditional_escape</span><span class="p">(</span><span class="n">error</span><span class="p">)</span> <span class="k">for</span> <span class="n">error</span> <span class="ow">in</span> <span class="n">bf</span><span class="o">.</span><span class="n">errors</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">bf</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">bf_errors</span><span class="p">:</span>
                    <span class="n">top_errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
                        <span class="p">[</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;(Hidden field </span><span class="si">%(name)s</span><span class="s1">) </span><span class="si">%(error)s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span> <span class="s1">&#39;error&#39;</span><span class="p">:</span> <span class="n">force_text</span><span class="p">(</span><span class="n">e</span><span class="p">)}</span>
                         <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">bf_errors</span><span class="p">])</span>
                <span class="n">hidden_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">bf</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Create a &#39;class=&quot;...&quot;&#39; attribute if the row should have any</span>
                <span class="c1"># CSS classes applied.</span>
                <span class="n">css_classes</span> <span class="o">=</span> <span class="n">bf</span><span class="o">.</span><span class="n">css_classes</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">css_classes</span><span class="p">:</span>
                    <span class="n">html_class_attr</span> <span class="o">=</span> <span class="s1">&#39; class=&quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">css_classes</span>

                <span class="k">if</span> <span class="n">errors_on_separate_row</span> <span class="ow">and</span> <span class="n">bf_errors</span><span class="p">:</span>
                    <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">error_row</span> <span class="o">%</span> <span class="n">force_text</span><span class="p">(</span><span class="n">bf_errors</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">bf</span><span class="o">.</span><span class="n">label</span><span class="p">:</span>
                    <span class="n">label</span> <span class="o">=</span> <span class="n">conditional_escape</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">bf</span><span class="o">.</span><span class="n">label</span><span class="p">))</span>
                    <span class="n">label</span> <span class="o">=</span> <span class="n">bf</span><span class="o">.</span><span class="n">label_tag</span><span class="p">(</span><span class="n">label</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;&#39;</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

                <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">help_text</span><span class="p">:</span>
                    <span class="n">help_text</span> <span class="o">=</span> <span class="n">help_text_html</span> <span class="o">%</span> <span class="n">force_text</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">help_text</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">help_text</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">normal_row</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s1">&#39;errors&#39;</span><span class="p">:</span> <span class="n">force_text</span><span class="p">(</span><span class="n">bf_errors</span><span class="p">),</span>
                    <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="n">force_text</span><span class="p">(</span><span class="n">label</span><span class="p">),</span>
                    <span class="s1">&#39;field&#39;</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">bf</span><span class="p">),</span>
                    <span class="s1">&#39;help_text&#39;</span><span class="p">:</span> <span class="n">help_text</span><span class="p">,</span>
                    <span class="s1">&#39;html_class_attr&#39;</span><span class="p">:</span> <span class="n">html_class_attr</span><span class="p">,</span>
                    <span class="s1">&#39;css_classes&#39;</span><span class="p">:</span> <span class="n">css_classes</span><span class="p">,</span>
                    <span class="s1">&#39;field_name&#39;</span><span class="p">:</span> <span class="n">bf</span><span class="o">.</span><span class="n">html_name</span><span class="p">,</span>
                <span class="p">})</span>

        <span class="k">if</span> <span class="n">top_errors</span><span class="p">:</span>
            <span class="n">output</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">error_row</span> <span class="o">%</span> <span class="n">force_text</span><span class="p">(</span><span class="n">top_errors</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">hidden_fields</span><span class="p">:</span>  <span class="c1"># Insert any hidden fields in the last row.</span>
            <span class="n">str_hidden</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">hidden_fields</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">output</span><span class="p">:</span>
                <span class="n">last_row</span> <span class="o">=</span> <span class="n">output</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="c1"># Chop off the trailing row_ender (e.g. &#39;&lt;/td&gt;&lt;/tr&gt;&#39;) and</span>
                <span class="c1"># insert the hidden fields.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">last_row</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">row_ender</span><span class="p">):</span>
                    <span class="c1"># This can happen in the as_p() case (and possibly others</span>
                    <span class="c1"># that users write): if there are only top errors, we may</span>
                    <span class="c1"># not be able to conscript the last row for our purposes,</span>
                    <span class="c1"># so insert a new, empty row.</span>
                    <span class="n">last_row</span> <span class="o">=</span> <span class="p">(</span><span class="n">normal_row</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s1">&#39;errors&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;field&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;help_text&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;html_class_attr&#39;</span><span class="p">:</span> <span class="n">html_class_attr</span><span class="p">,</span>
                        <span class="s1">&#39;css_classes&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;field_name&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                    <span class="p">})</span>
                    <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">last_row</span><span class="p">)</span>
                <span class="n">output</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">last_row</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">row_ender</span><span class="p">)]</span> <span class="o">+</span> <span class="n">str_hidden</span> <span class="o">+</span> <span class="n">row_ender</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># If there aren&#39;t any rows in the output, just append the</span>
                <span class="c1"># hidden fields.</span>
                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">str_hidden</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">as_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns this form rendered as HTML &lt;tr&gt;s -- excluding the &lt;table&gt;&lt;/table&gt;.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_html_output</span><span class="p">(</span>
            <span class="n">normal_row</span><span class="o">=</span><span class="s1">&#39;&lt;tr</span><span class="si">%(html_class_attr)s</span><span class="s1">&gt;&lt;th&gt;</span><span class="si">%(label)s</span><span class="s1">&lt;/th&gt;&lt;td&gt;</span><span class="si">%(errors)s%(field)s%(help_text)s</span><span class="s1">&lt;/td&gt;&lt;/tr&gt;&#39;</span><span class="p">,</span>
            <span class="n">error_row</span><span class="o">=</span><span class="s1">&#39;&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;</span><span class="si">%s</span><span class="s1">&lt;/td&gt;&lt;/tr&gt;&#39;</span><span class="p">,</span>
            <span class="n">row_ender</span><span class="o">=</span><span class="s1">&#39;&lt;/td&gt;&lt;/tr&gt;&#39;</span><span class="p">,</span>
            <span class="n">help_text_html</span><span class="o">=</span><span class="s1">&#39;&lt;br /&gt;&lt;span class=&quot;helptext&quot;&gt;</span><span class="si">%s</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="n">errors_on_separate_row</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">as_ul</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns this form rendered as HTML &lt;li&gt;s -- excluding the &lt;ul&gt;&lt;/ul&gt;.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_html_output</span><span class="p">(</span>
            <span class="n">normal_row</span><span class="o">=</span><span class="s1">&#39;&lt;li</span><span class="si">%(html_class_attr)s</span><span class="s1">&gt;</span><span class="si">%(errors)s%(label)s</span><span class="s1"> </span><span class="si">%(field)s%(help_text)s</span><span class="s1">&lt;/li&gt;&#39;</span><span class="p">,</span>
            <span class="n">error_row</span><span class="o">=</span><span class="s1">&#39;&lt;li&gt;</span><span class="si">%s</span><span class="s1">&lt;/li&gt;&#39;</span><span class="p">,</span>
            <span class="n">row_ender</span><span class="o">=</span><span class="s1">&#39;&lt;/li&gt;&#39;</span><span class="p">,</span>
            <span class="n">help_text_html</span><span class="o">=</span><span class="s1">&#39; &lt;span class=&quot;helptext&quot;&gt;</span><span class="si">%s</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="n">errors_on_separate_row</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">as_p</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns this form rendered as HTML &lt;p&gt;s.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_html_output</span><span class="p">(</span>
            <span class="n">normal_row</span><span class="o">=</span><span class="s1">&#39;&lt;p</span><span class="si">%(html_class_attr)s</span><span class="s1">&gt;</span><span class="si">%(label)s</span><span class="s1"> </span><span class="si">%(field)s%(help_text)s</span><span class="s1">&lt;/p&gt;&#39;</span><span class="p">,</span>
            <span class="n">error_row</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span>
            <span class="n">row_ender</span><span class="o">=</span><span class="s1">&#39;&lt;/p&gt;&#39;</span><span class="p">,</span>
            <span class="n">help_text_html</span><span class="o">=</span><span class="s1">&#39; &lt;span class=&quot;helptext&quot;&gt;</span><span class="si">%s</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="n">errors_on_separate_row</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">non_field_errors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns an ErrorList of errors that aren&#39;t associated with a particular</span>
<span class="sd">        field -- i.e., from Form.clean(). Returns an empty ErrorList if there</span>
<span class="sd">        are none.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">NON_FIELD_ERRORS</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">(</span><span class="n">error_class</span><span class="o">=</span><span class="s1">&#39;nonfield&#39;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">add_error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">error</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update the content of `self._errors`.</span>

<span class="sd">        The `field` argument is the name of the field to which the errors</span>
<span class="sd">        should be added. If its value is None the errors will be treated as</span>
<span class="sd">        NON_FIELD_ERRORS.</span>

<span class="sd">        The `error` argument can be a single error, a list of errors, or a</span>
<span class="sd">        dictionary that maps field names to lists of errors. What we define as</span>
<span class="sd">        an &quot;error&quot; can be either a simple string or an instance of</span>
<span class="sd">        ValidationError with its message attribute set and what we define as</span>
<span class="sd">        list or dictionary can be an actual `list` or `dict` or an instance</span>
<span class="sd">        of ValidationError with its `error_list` or `error_dict` attribute set.</span>

<span class="sd">        If `error` is a dictionary, the `field` argument *must* be None and</span>
<span class="sd">        errors will be added to the fields that correspond to the keys of the</span>
<span class="sd">        dictionary.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">):</span>
            <span class="c1"># Normalize to ValidationError and let its constructor</span>
            <span class="c1"># do the hard work of making sense of the input.</span>
            <span class="n">error</span> <span class="o">=</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s1">&#39;error_dict&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                    <span class="s2">&quot;The argument `field` must be `None` when the `error` &quot;</span>
                    <span class="s2">&quot;argument contains errors for multiple fields.&quot;</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="o">.</span><span class="n">error_dict</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">error</span> <span class="o">=</span> <span class="p">{</span><span class="n">field</span> <span class="ow">or</span> <span class="n">NON_FIELD_ERRORS</span><span class="p">:</span> <span class="n">error</span><span class="o">.</span><span class="n">error_list</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">error_list</span> <span class="ow">in</span> <span class="n">error</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">field</span> <span class="o">!=</span> <span class="n">NON_FIELD_ERRORS</span> <span class="ow">and</span> <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; has no field named &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">field</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">field</span> <span class="o">==</span> <span class="n">NON_FIELD_ERRORS</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">(</span><span class="n">error_class</span><span class="o">=</span><span class="s1">&#39;nonfield&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_class</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span><span class="p">[</span><span class="n">field</span><span class="p">]</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">error_list</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">:</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">has_error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span>
        <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">error</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">as_data</span><span class="p">()[</span><span class="n">field</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">error</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">code</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">full_clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cleans all of self.data and populates self._errors and</span>
<span class="sd">        self.cleaned_data.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_errors</span> <span class="o">=</span> <span class="n">ErrorDict</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_bound</span><span class="p">:</span>  <span class="c1"># Stop further processing.</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># If the form is permitted to be empty, and none of the form data has</span>
        <span class="c1"># changed from the initial data, short circuit any validation.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">empty_permitted</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_changed</span><span class="p">():</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_clean_fields</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_clean_form</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_post_clean</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_clean_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># value_from_datadict() gets the data from the data dictionaries.</span>
            <span class="c1"># Each widget type knows how to retrieve its own data, because some</span>
            <span class="c1"># widgets split data over several HTML fields.</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">disabled</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_initial_for_field</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">files</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">FileField</span><span class="p">):</span>
                    <span class="n">initial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_initial_for_field</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">initial</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;clean_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">):</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;clean_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
            <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">add_error</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_clean_form</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">cleaned_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_error</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">cleaned_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="o">=</span> <span class="n">cleaned_data</span>

    <span class="k">def</span> <span class="nf">_post_clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        An internal hook for performing additional cleaning after form cleaning</span>
<span class="sd">        is complete. Used for model validation in model forms.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for doing any extra form-wide cleaning after Field.clean() has been</span>
<span class="sd">        called on every field. Any ValidationError raised by this method will</span>
<span class="sd">        not be associated with a particular field; it will have a special-case</span>
<span class="sd">        association with the field named &#39;__all__&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span>

    <span class="k">def</span> <span class="nf">has_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if data differs from initial.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">changed_data</span><span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">changed_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">prefixed_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="n">data_value</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">files</span><span class="p">,</span> <span class="n">prefixed_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">show_hidden_initial</span><span class="p">:</span>
                <span class="c1"># Use the BoundField&#39;s initial as this is the value passed to</span>
                <span class="c1"># the widget.</span>
                <span class="n">initial_value</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">initial</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">initial_prefixed_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_initial_prefix</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                <span class="n">hidden_widget</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">hidden_widget</span><span class="p">()</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">initial_value</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">to_python</span><span class="p">(</span><span class="n">hidden_widget</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">files</span><span class="p">,</span> <span class="n">initial_prefixed_name</span><span class="p">))</span>
                <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
                    <span class="c1"># Always assume data has changed if validation fails.</span>
                    <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">continue</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">has_changed</span><span class="p">(</span><span class="n">initial_value</span><span class="p">,</span> <span class="n">data_value</span><span class="p">):</span>
                <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provide a description of all media required to render the widgets on this form</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">media</span> <span class="o">=</span> <span class="n">Media</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">media</span> <span class="o">=</span> <span class="n">media</span> <span class="o">+</span> <span class="n">field</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">media</span>
        <span class="k">return</span> <span class="n">media</span>

    <span class="k">def</span> <span class="nf">is_multipart</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if the form needs to be multipart-encoded, i.e. it has</span>
<span class="sd">        FileInput. Otherwise, False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">needs_multipart_form</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">hidden_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of all the BoundField objects that are hidden fields.</span>
<span class="sd">        Useful for manual form layout in templates.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">field</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">visible_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of BoundField objects that aren&#39;t hidden fields.</span>
<span class="sd">        The opposite of the hidden_fields() method.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">field</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_initial_for_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return initial data for field on form. Use initial data from the form</span>
<span class="sd">        or the field, in that order. Evaluate callable values.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">initial</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">value</span>


<div class="viewcode-block" id="Form"><a class="viewcode-back" href="../../../ref/forms/api.html#django.forms.Form">[docs]</a><span class="k">class</span> <span class="nc">Form</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">DeclarativeFieldsMetaclass</span><span class="p">,</span> <span class="n">BaseForm</span><span class="p">)):</span>
    <span class="s2">&quot;A collection of Fields, plus their associated data.&quot;</span></div>
    <span class="c1"># This is a separate class from BaseForm in order to abstract the way</span>
    <span class="c1"># self.fields is specified. This class (Form) is the one that does the</span>
    <span class="c1"># fancy metaclass stuff purely for the semantic sugar -- it allows one</span>
    <span class="c1"># to define a form using declarative syntax.</span>
    <span class="c1"># BaseForm itself has no way of designating self.fields.</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>